package com.shinowit.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shinowit.dao.mapper.SysUserMapper;
import com.shinowit.model.DataGridResult;
import com.shinowit.model.Result;
import com.shinowit.model.SysUser;
import com.shinowit.model.SysUserCriteria;
import com.shinowit.service.UserService;

@Controller
@RequestMapping("/system/user")
public class UserManagerController {
	
	@Resource
	private JdbcTemplate jt;
	
	@Resource
	private SysUserMapper sys_user_dao;
	
	@Resource
	private UserService user_service;
	
	public  DataGridResult<Map<String,Object>> queryDataByPage(Integer pageSize,Integer pageIndex,SysUser user){
		DataGridResult<Map<String,Object>> result=new DataGridResult<Map<String,Object>>();
		String select_sql="select b.org_name,a.user_code,a.org_code,a.user_name,a.phone,a.mobile_phone,a.email,a.memo, a.order_id,a.status,"
				+ " (select GROUP_CONCAT(z.sys_role_name Separator ',') from sys_role z inner join sys_user_role y on z.sys_role_code=y.sys_role_code where y.user_code=a.user_code ) as roles,"
				+ "(select GROUP_CONCAT(z.sys_role_code Separator ',') from sys_role z inner join sys_user_role y on z.sys_role_code=y.sys_role_code where y.user_code=a.user_code ) as role_codes "
				+ "  from  sys_user a inner join sys_org_info b on a.org_code=b.org_code ";
		String count_sql="select count(0) from sys_user a ";
		List<String> query_param=new ArrayList<String>();
		Map<String,Object> query_param_value_map=new HashMap<String,Object>();
		
		if (null!=user){
			select_sql=select_sql+" where 1=1 ";
			count_sql=count_sql+" where 1=1 ";
			if ((user.getUserCode()!=null) && (user.getUserCode().trim().length()>0)){
				query_param.add("and  a.user_code like :user_code");
				query_param_value_map.put("user_code", user.getUserCode()+"%");
			}
			if ((user.getUserName()!=null) && (user.getUserName().trim().length()>0)){
				query_param.add("and  a.user_name like :user_name");
				query_param_value_map.put("user_name", user.getUserName()+"%");
			}
		}
		
		if (query_param.size()>0){
			for (String s:query_param){
				select_sql=select_sql+" "+s+" ";
				count_sql=count_sql+" "+s+" ";
			}
		}
		
		
		select_sql=select_sql+" order by a.user_code limit :skip_count,:pageSize";
		query_param_value_map.put("skip_count", (pageIndex-1)*pageSize);
		query_param_value_map.put("pageSize", pageSize);
		
		
		NamedParameterJdbcTemplate  njt=new NamedParameterJdbcTemplate(jt);
		int count=njt.queryForObject(count_sql, query_param_value_map, Integer.class);
		result.setTotal(count);
		
		//List<Map<String,Object>> rows=jt.queryForList(select_sql,new Object[]{(pageIndex-1)*pageSize,pageSize},new int[]{Types.INTEGER,Types.INTEGER});
		List<Map<String,Object>> rows=njt.queryForList(select_sql,query_param_value_map);		
		result.setRows(rows);
		return result;
	}
	
	@RequestMapping(value={"/",""})
	public String defaultPage(Model model){
		return "/user/list";
	}

	@RequestMapping(value="/page")
	@ResponseBody
	public DataGridResult<Map<String,Object>> getPageData(Model model,Integer page,Integer rows,SysUser user){
		return queryDataByPage(rows,page,user);
	}

	@RequestMapping(value="/role_list")
	@ResponseBody
	public List<Map<String,Object>>  getAllRoleList(){
		return jt.queryForList("select sys_role_code as id,sys_role_name as text from sys_role ");
	}
	
	@RequestMapping(value="/create")
	@ResponseBody
	public Result create_new_user(@RequestBody SysUser user){
		Result result=new Result("未知错误!",false);
		try{
			
			//检查有无重复数据
			SysUserCriteria ex=new SysUserCriteria();
			ex.createCriteria().andUserNameEqualTo(user.getUserName());
			
			long tmp_i=sys_user_dao.countByExample(ex);
			if (tmp_i>0){
				result.setMsg("用户名重复!无法新建用户,请更换其它用户名!");
				return result;
			}
			
			boolean status=user_service.createUser(user);
			if (true==status){
				result.setMsg("新建用户成功!");
				result.setStatus(true);
			}else{
				result.setMsg("新建用户失败!");
			}
		}catch(Exception e){
			e.printStackTrace();
			result.setMsg("新建用户失败!");
		}
		return result;
	}
	
	@RequestMapping(value="/update")
	@ResponseBody
	public Result update_user(@RequestBody SysUser user){
		Result result=new Result("未知错误!",false);
		try{
			boolean status=user_service.editUser(user);
			if (true==status){
				result.setMsg("编辑用户信息成功!");
				result.setStatus(true);
			}else{
				result.setMsg("编辑用户信息失败!");
			}
		}catch(Exception e){
			e.printStackTrace();
			result.setMsg("编辑用户信息失败!");
		}
		return result;
	}	
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Result delete_user(SysUser user){
		Result result=new Result("未知错误!",false);
		try{
			boolean status=user_service.deleteUser(user);
			if (true==status){
				result.setMsg("删除用户信息成功!");
				result.setStatus(true);
			}else{
				result.setMsg("删除用户信息失败!");
			}
		}catch(Exception e){
			e.printStackTrace();
			result.setMsg("删除用户信息失败!");
		}
		return result;		
	}
	
}
